# -*- coding: utf-8 -*-
from django.contrib import messages
from django.contrib.auth import login
from django.shortcuts import render, redirect
from django.utils.translation import ugettext as _

from userprofiles import settings as up_settings
from userprofiles.contrib.accountverification.models import AccountVerification


def registration_activate(request, activation_key):
    activation_key = activation_key.lower()
    account = AccountVerification.objects.activate_user(activation_key)

    if account:
        account.backend = 'django.contrib.auth.backends.ModelBackend'
        login(request, account)
        messages.success(request, _(u'Bienvenido a No-Name. Hemos activado exitosamente su cuenta.'))
        return redirect('companies_index_db_company')

    return render(request, 'userprofiles/registration_activate.html', {
        'account': account,
        'expiration_days': up_settings.ACCOUNT_VERIFICATION_DAYS
    })
